package cn.signalpha.modules.shop.controller.seller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.log.exception.ServiceException;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.StringPool;
import cn.signalpha.core.tool.utils.StringUtil;
import cn.signalpha.modules.shop.dto.PatternGoodsDTO;
import cn.signalpha.modules.shop.entity.Goods;
import cn.signalpha.modules.shop.entity.Pattern;
import cn.signalpha.modules.shop.entity.PatternGoods;
import cn.signalpha.modules.shop.service.GoodsService;
import cn.signalpha.modules.shop.service.PatternGoodsService;
import cn.signalpha.modules.shop.service.PatternService;
import cn.signalpha.modules.shop.vo.PatternGoodsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/sys-seller/pattern/goods")
@Api(value = "运营模式商品", tags = "运营模式商品")
public class SpiPatternGoodsController extends BaseController {

	private final GoodsService goodsService;
	private final PatternService patternService;
	private final PatternGoodsService patternGoodsService;

    @GetMapping("/page")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id", dataType = "string", value = "主键"),
        @ApiImplicitParam(name="goodsId", dataType = "string", value = "商品id"),
        @ApiImplicitParam(name="patternId", dataType = "string", value = "运营模式id"),
        @ApiImplicitParam(name="patternType", dataType = "string", value = "运营模式类型"),
    })
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<PatternGoodsVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("sellerId", getUser().getTenantId());
        return R.data(patternGoodsService.selectPatternGoodsPage(Condition.getPage(query), params));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<PatternGoods> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(patternGoodsService.getById(id));
	}

	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入patternGoods")
	public R save(@Valid @RequestBody PatternGoodsDTO.SaveDTO saveDTO) {
		Pattern pattern = patternService.getById(saveDTO.getPatternId());
		Assert.isNull(pattern, "运营模式不存在");
		Goods goods = goodsService.getById(saveDTO.getGoodsId());
		Assert.isNull(goods, "商品不存在");
		if(patternGoodsService.isExists(saveDTO.getPatternId(), goods.getId())){
			throw new ServiceException(StringUtil.format("商品{}重复", goods.getName()));
		}
		PatternGoods patternGoods = new PatternGoods();
		patternGoods.setSellerId(Long.valueOf(getUser().getTenantId()));
		patternGoods.setPatternId(pattern.getId());
		patternGoods.setPatternType(pattern.getType());
		patternGoods.setGoodsId(goods.getId());
		patternGoods.setConfig(StringUtil.isBlank(saveDTO.getConfig()) ? StringPool.EMPTY_JSON : saveDTO.getConfig());
		patternGoods.setCreateTime(new Date());
		patternGoodsService.save(patternGoods);
		return R.status(true);
	}

	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入patternGoods")
	public R update(@Valid @RequestBody PatternGoodsDTO.UpdateDTO patternGoods) {
		return R.status(patternGoodsService.updateById(BeanUtil.copy(patternGoods, PatternGoods.class)));
	}

	@PostMapping("/batch/save")
	@ApiOperation(value = "批量新增", notes = "传入patternGoods")
	public R batchSave(@Valid @RequestBody PatternGoodsDTO.BatchSaveDTO saveDTO) {
		return R.status(true);
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(patternGoodsService.removeByIds(Func.toLongList(ids)));
	}

}
